package com.suning.sawp.service.impl.oga;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.dto.oga.bi.DevelopingActivitySale;
import com.suning.sawp.dto.oga.bi.SimpleRange;
import com.suning.sawp.dto.oga.bi.StaffDevelopingActivitySale;
import com.suning.sawp.po.bi.BiDevelopingActivitySale;
import com.suning.sawp.po.bi.BiStaffDevelopingActivitySaleMonthEntity;
import com.suning.sawp.po.bi.BiStoreDevelopingActivitySaleMonthEntity;
import com.suning.sawp.service.impl.oga.bi.support.Populates;
import com.suning.sawp.service.impl.oga.bi.support.SimpleRanges;

/**
 * 
 * 外拓拓分活动BI统计
 * 
 * @author 13071602
 *
 */
@Service("biDevelopingActivitySaleService")
public class BIDevelopingActivitySaleService {

    @Autowired
    protected DalClient dalClient;

    /**
     * 门店外拓任务销售统计详情（各个店员）
     * 
     * @param activityCode
     * @return
     */
    public List<StaffDevelopingActivitySale> queryStaffsDevelopingActivitySaleInfo(String storeCode,
            Integer activityCode) {
        return Lists.transform(queryStaffsDevelopingActivitySale(storeCode, activityCode),
                new Function<BiStaffDevelopingActivitySaleMonthEntity, StaffDevelopingActivitySale>() {
                    @Override
                    public StaffDevelopingActivitySale apply(BiStaffDevelopingActivitySaleMonthEntity input) {
                        StaffDevelopingActivitySale storeDevelopingActivitySale = new StaffDevelopingActivitySale();
                        populateStoreDevelopingActivitySale(storeDevelopingActivitySale, input);
                        Populates.populateStaffInfo(storeDevelopingActivitySale, input);
                        return storeDevelopingActivitySale;
                    }
                });
    }

    /**
     * 门店维度外拓销售统计
     * 
     * @param offset
     * @param limit
     * @param online
     * @return
     */
    public List<DevelopingActivitySale> queryStoreDevelopingActivitySaleInfo(String storeCode, int offset, int limit,
            boolean online) {
        return Lists.transform(queryStoreDevelopingActivitySale(storeCode, offset, limit, online),
                new Function<BiStoreDevelopingActivitySaleMonthEntity, DevelopingActivitySale>() {
                    @Override
                    public DevelopingActivitySale apply(BiStoreDevelopingActivitySaleMonthEntity input) {
                        DevelopingActivitySale storeDevelopingActivitySale = new DevelopingActivitySale();
                        populateStoreDevelopingActivitySale(storeDevelopingActivitySale, input);
                        return storeDevelopingActivitySale;
                    }
                });
    }

    /**
     * 店员维度外拓销售统计
     * 
     * @param offset
     * @param limit
     * @param online
     * @return
     */
    public List<DevelopingActivitySale> queryStaffDevelopingActivitySaleInfo(String storeCode, String staffId,
            int offset, int limit, boolean online) {
        return Lists.transform(queryStaffDevelopingActivitySale(storeCode, staffId, offset, limit, online),
                new Function<BiStaffDevelopingActivitySaleMonthEntity, DevelopingActivitySale>() {
                    @Override
                    public DevelopingActivitySale apply(BiStaffDevelopingActivitySaleMonthEntity input) {
                        DevelopingActivitySale storeDevelopingActivitySale = new DevelopingActivitySale();
                        populateStoreDevelopingActivitySale(storeDevelopingActivitySale, input);
                        return storeDevelopingActivitySale;
                    }
                });
    }

    private List<BiStoreDevelopingActivitySaleMonthEntity> queryStoreDevelopingActivitySale(String storeCode,
            int offset, int limit, boolean online) {
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("online", online);
        paramMap.put("storeCode", storeCode);
        paramMap.put("offset", offset);
        paramMap.put("limit", limit);
        populateMonthActivityRange(paramMap);
        return dalClient.queryForList("BIDevelopingActivitySaleService.queryStoreDevelopingActivitySale", paramMap,
                BiStoreDevelopingActivitySaleMonthEntity.class);
    }

    private List<BiStaffDevelopingActivitySaleMonthEntity> queryStaffsDevelopingActivitySale(String storeCode,
            Integer activityCode) {
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("storeCode", storeCode);
        paramMap.put("activityCode", activityCode);
        return dalClient.queryForList("BIDevelopingActivitySaleService.queryStaffsDevelopingActivitySale", paramMap,
                BiStaffDevelopingActivitySaleMonthEntity.class);
    }

    private List<BiStaffDevelopingActivitySaleMonthEntity> queryStaffDevelopingActivitySale(String storeCode,
            String staffId, int offset, int limit, boolean online) {
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("storeCode", storeCode);
        paramMap.put("staffId", staffId);
        paramMap.put("online", online);
        paramMap.put("offset", offset);
        paramMap.put("limit", limit);
        populateMonthActivityRange(paramMap);
        return dalClient.queryForList("BIDevelopingActivitySaleService.queryStaffDevelopingActivitySale", paramMap,
                BiStaffDevelopingActivitySaleMonthEntity.class);
    }

    private void populateMonthActivityRange(Map<String, Object> paramMap) {
        SimpleRange<LocalDate> range = SimpleRanges.monthWithLocalDate(new LocalDate().minusDays(1));
        paramMap.put("lowerBound", range.getLowerBound().toDateTime(new LocalTime(00, 00, 00)).toDate());
        paramMap.put("upperBound", range.getUpperBound().toDateTime(new LocalTime(23, 59, 59)).toDate());
    }

    private void populateStoreDevelopingActivitySale(DevelopingActivitySale dest, BiDevelopingActivitySale orig) {
        dest.setActivityId(orig.getActivityCode());
        dest.setActivityName(orig.getActivityName());
        dest.setDevelop(orig.getDevelopNum());
        dest.setDeveloped(orig.getDevelopedNum());
        dest.setDevelopedOrder(orig.getDevelopedOrder());
        dest.setDevelopedRate(orig.getDevelopedRate());
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dest.setStartTime(dateFormat.format(orig.getActivityStartTime()));
        dest.setEndTime(dateFormat.format(orig.getActivityEndTime()));
        dest.setSaleAmount(orig.getDevelopedSale());
    }

}
